package com.gla.giliair.web.customer;

import com.gla.giliair.entity.Seat;
import com.gla.giliair.util.IndentityUtil;
import com.gla.giliair.util.MapGenerator;
import com.gla.giliair.util.PatternUtil;
import com.gla.giliair.web.BaseController;
import com.gla.giliair.web.vo.CustomerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/25 20:42
 */
@Api(tags = "座位模块")
@RestController
@RequestMapping("/api/user")
public class SeatController extends BaseController {
    @ApiOperation(value = "获取携带订单信息的动态座位列表", notes = "根据航班号和身份证号，获取携带订单信息的动态座位列表", produces = "application/json")
    @PostMapping("/seatListWithOrder")
    @ResponseBody
    public Map<String, Object> getSeatListWithOrder(@RequestParam("flightId") Long flightId,
                                                    @RequestParam("IDNumber") String IDNumber,
                                                    @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pageSize", defaultValue = "300") Integer pageSize) {
        if (flightId == null) {
            return MapGenerator.getRes(500, "请输入航班号！");
        }
        if (!StringUtils.hasText(IDNumber)) {
            return MapGenerator.getRes(501, "请输入身份证号！");
        }
        if (!PatternUtil.isIdentity(IDNumber)) {
            return MapGenerator.getRes(502, "乘机人身份证号格式有误！");
        }
        return seatService.findSeatsWithOrderByFlightIdAndIDNumber(flightId, IDNumber, page, pageSize);
    }

    @ApiOperation(value = "获取动态座位列表", notes = "根据航班号，获取动态座位列表", produces = "application/json")
    @GetMapping("/seatList")
    @ResponseBody
    public Map<String, Object> findSeatList(@RequestParam("flightId") Long flightId,
                                           @RequestParam(value = "page", defaultValue = "1") Integer page,
                                           @RequestParam(value = "pageSize", defaultValue = "300") Integer pageSize) {
        if (flightId == null) {
            return MapGenerator.getRes(500, "请输入航班号！");
        }
        return seatService.findSeatsByFlightId(flightId, page, pageSize);
    }

    @ApiOperation(value = "选定座位", notes = "选定座位", produces = "application/json")
    @PostMapping("/selectSeat")
    @ResponseBody
    public Map<String, Object> selectSeat(@RequestParam("seatId") Long seatId,
                                          @RequestParam("orderId") Long orderId,
                                          HttpSession httpSession) {
        if (seatId == null) {
            return MapGenerator.getRes(500, "请输入座位号！");
        }
        if (orderId == null) {
            return MapGenerator.getRes(501, "请输入订单号！");
        }
        CustomerVO userTemp = (CustomerVO) httpSession.getAttribute("user");

        Seat seat = new Seat();
        seat.setSeatId(seatId);
        seat.setOrderId(orderId);

        return seatService.selectSeat(seat, userTemp.getCustomerId());
    }

    @ApiOperation(value = "取消座位", notes = "取消座位", produces = "application/json")
    @PostMapping("/cancelSeat")
    @ResponseBody
    public Map<String, Object> cancelSeat(@RequestParam("orderId") Long orderId,
                                          HttpSession httpSession) {
        if (orderId == null) {
            return MapGenerator.getRes(501, "请输入订单号！");
        }
        CustomerVO userTemp = (CustomerVO) httpSession.getAttribute("user");

        Seat seat = new Seat();
        seat.setOrderId(orderId);

        return seatService.cancelSeat(seat, userTemp.getCustomerId());
    }
}
